#ifndef _WINDOW_HANDLER_WIN32_H_
#define _WINDOW_HANDLER_WIN32_H_

#include "ui/ui_widget.h"
#include "num/named_set.h"
#include "window_handler.h"
#ifdef _PLATFORM_WIN32
#include <windows.h>

class	WindowHandlerWin32 : public WindowHandler
{
public:
						WindowHandlerWin32(HINSTANCE	hInst);
	virtual				~WindowHandlerWin32();
// all windows need to handle the following events.
	virtual bool		InitWindow(const std::string& Name, const std::string& Title, int	X, int Y, int Width, int Height) ;
	virtual bool		InitGraphics();
	virtual bool		DestroyWindow() ;
	virtual bool		Tick(float dt);
	virtual void		ResetDevice();
	virtual bool		UpdateOS();

// not applicable to all, but for most
	virtual bool		Minimize();
	virtual bool		Maximize();
	virtual void		SwitchFullscreen(int Width, int Height) ;
	virtual void		SwitchWindowed(int Width, int Height);
	virtual std::string	GetSaveFolder();
	virtual bool		OnGainFocus() ;
	virtual bool		OnLooseFocus() ;


	// Override these in your own handlers if you need them
	virtual HICON		GetApplicationIcon() const		{ return ::LoadIcon ( 0, IDI_APPLICATION ); }
	virtual HICON		GetApplicationIconSm() const	{ return ::LoadIcon ( 0, IDI_WINLOGO ); }
	virtual	bool		OnFileDrag(const std::string& File) { return true; };


// functions to change screen resolution around
	virtual bool		GetCurrentResolution(unsigned int& Width, unsigned int& Height, unsigned int &Depth);
	virtual bool		SetResolution(unsigned int Width, unsigned int Height, unsigned int Depth);
	virtual bool		DetectWidescreenMode();
	virtual bool		RestoreResolution();

// mouse and cursor functions
	virtual bool		SetCursor(const std::string& File);
	inline math::Vec2	MapPointToPos(int x, int y);

protected:

	HICON				InitIcon( const std::string& Name);
	HICON				InitIcon( long	IconID );
	HCURSOR				InitCursor( const std::string& Name);

	void				RecalcWindowSize();
	void DELEGATE_CALL	OnHardQuit(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget);

	LRESULT				WndProc ( HWND _hwnd, UINT _msg, WPARAM _wparam, LPARAM _lparam );
	static LRESULT CALLBACK defWndProc ( HWND _hwnd, UINT _msg, WPARAM _wparam, LPARAM _lparam );
	HWND				m_hwnd;
	HINSTANCE			m_Instance;
	WNDCLASSEX			m_Class;
	HWND				m_hWndParent;    // handle to parent or owner window
	HMENU				m_hMenu;         // handle to menu, or child-window ID
	unsigned int		m_Style, m_ExtendedStyle;
	HCURSOR				m_hCursor;
	RECT				m_WindowRect;
	std::string			m_CursorName;

	static WindowHandlerWin32*	pSelf;

	struct				CursorData
	{
		CursorData() :
			Cursor(NULL)
		{

		}
		~CursorData()
		{
			DestroyCursor(Cursor);
		}
		HCURSOR				Cursor;
		std::string			Name;
	};
	num::NamedSet<CursorData*>	m_CursorData;

private:
	void				ResetMouseTracking();
	bool				m_MouseTrackingSet;
};

#endif //_PLATFORM_WIN32
#endif //_WINDOW_HANDLER_WIN32_H_